package com.firstjianmo.fragmenttest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                changeRightFragment();
                break;
        }
    }

    private void changeRightFragment() {
        String inputText = getInputText();
        addAnotherRightFragment(inputText);
    }

    private void addAnotherRightFragment(String inputText) {
        AnotherRightFragment fragment = new AnotherRightFragment(inputText);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public String getInputText() {
        EditText inputEdit = (EditText) findViewById(R.id.input_text);

        String inputText = inputEdit.getText().toString();

        return inputText;
    }
}
